<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Info; 
use App\Org\Image;

class InfoController extends Controller
{
    public function index ()
    {
    	$list = Info::get();
    	// dd($list);
    	return view('admin.info.index',['list'=>$list]);
    }
    public function create()
    {
    	return view('admin.info.create');
    }

    public function store(Request $request)
    {
        // dd($request);
        $data = $request->except('_token');
        // dd($data);
        if ($request->hasFile('photo')) {
            if ($request->file('photo')->isValid()) {
                $file = $request->file('photo');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                // dd($ext);
                $photo = time().rand(1000,9999).'.'.$ext;
                $file->move('./uploads/',$photo);
                if($file->getError()>0){ 
                    echo '上传失败';
                }else{
                    echo '上传成功';
                }
            }
        }else{
            echo "<script>alert('请选择图片');</script>";
            return redirect('admin/info/create');
        }    
        $data['photo'] = $photo;        
        $row = Info::insertGetId($data);        
        if($row>0){
            return redirect('admin/info');
        }
    }

    public function edit($id)
    {
        $list = Info::where('id',$id)->first();
        return view('admin.info.edit',['list'=>$list]);
    }

    public function update($id,Request $request)
    {
        // dd($request);
        $data = $request->except('_token','_method');
        $lsit = Info::where('id',$id)->update($data);
        if($lsit>0){
            return redirect('admin/info');
        }else{
            return back()->with('msg','修改失败');
        }
    }

    public function destroy($id)
    {
        $list = Info::where('id',$id)->delete();
        if($list>0){
            return redirect('admin/info');
        }
    }
}
